﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shape;
using Shape.Mvp.Sample.Views;
using Shape.Mvp.Sample.Domain.Repositories;
using Shape.Mvp.Sample.Events;
using Shape.Mvp.Sample.Domain.Model;

namespace Shape.Mvp.Sample.Presenters
{
	public class PositionListPresenter : Presenter<IPositionListView>
	{
		private IRepository<Position> _positionRepository;

		public PositionListPresenter(
			IApplicationController applicationController,
         IRepository<Position> repository, 
         IPositionListView view) : base(applicationController, view)
		{
			_positionRepository = repository;
		}

		public void Initialise()
      {
			// Attach events
			View.Accepted += OnViewAccepted;

			View.ClearPositions();

			foreach (var item in _positionRepository.GetAll())
			{
				View.AddPosition(item.Id.ToString(), item.Description);
			}
		}

#region Events
		void OnViewAccepted(object sender, EventArgs e)
		{
			View.CanClose = true;

			// Broadcast that the exercise has been updated
			ApplicationController.Publish<PositionSelectedEvent>
			(
				new PositionSelectedEvent(Guid.Parse(View.SelectedPosition()))
			);
			
		}
#endregion

	}
}
